var CategoryData = Class.create({
  initialize:function(elem,type,item)
  {
      this.url="/accountList.json";
      this.elem = $(elem);
      this.type = type;
      this.item = item;
      this.loadData();
  },
  setCategoriesData:function(categories)
  {
    var optStr=null;
    if(this.type == "init" && categories.length == 0)
    {
      optStr = '<span style="color:red;font-size:10px">項目がありません。登録・編集メニューの項目を選択して、食費、交通費のような項目を作成してください。</span>' 
      optStr += '<br>項目<select name="category" id="category"></select>';
    }
    else
    {
      optStr = '項目<select name="category" id="category">';
      if(this.type != "init")
      {
        optStr += '<option value="" >指定なし</option>';
      }
      for (var i=0 ;i<categories.length;i++)
      {
        var selected="";
        if(categories[i].name == this.item)
        {
          selected = 'selected="selected"';
        }
        optStr += '<option value="'+categories[i].key+'" '+ selected + ' >'+categories[i].name+'</option>';
      }
      optStr+='</select>';
    }
    $("items_span").update(optStr);
  },
  loadData:function()
  {
    var a = new Ajax.Request( 
        "/categories.json", 
        { 
            "method": "get", 
            "parameters":makeTemporaryQuery(),
            "sanitizeJSON":true,
            onSuccess: function(request) { 
              var dataSet = request.responseText.evalJSON();
              this.setCategoriesData(dataSet);
            }.bind(this), 
            onFailure: function(request) { 
                alert('読み込みに失敗しました'); 
            }, 
            onException: function (request) { 
                alert('読み込み中にエラーが発生しました'); 
            } 
        }); 
  }
});
